# __author__ = 'heyin'
# __date__ = '2018/11/9 11:45'
import random
import numpy as np

from pyecharts import Scatter
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.metrics import silhouette_score


def plot(d, name):
    v1 = d[:, 0]
    v2 = d[:, 1]
    scatter = Scatter("散点图示例")
    scatter.add("A", v1, v2)
    scatter.render(path=name)


def k():
    # num1 = range(33)
    # num2 = range(33, 66)
    # num3 = range(66, 100)
    # num = range(100)
    # o_data = list()
    # for i in range(100):
    #     o_data.append([random.choice(num), random.choice(num)])
    # if i < 33:
    #     o_data.append([random.choice(num1), random.choice(num1)])
    # elif i < 66:
    #     o_data.append([random.choice(num2), random.choice(num2)])
    # else:
    #     o_data.append([random.choice(num3), random.choice(num3)])
    o_data = [[60, 63], [66, 49], [63, 51], [47, 8], [79, 10], [78, 54], [44, 14], [0, 50], [78, 98], [81, 33],
              [72, 85], [49, 58], [58, 44], [83, 38], [69, 63], [59, 95], [19, 45], [32, 88], [77, 50], [78, 83],
              [17, 20], [88, 68], [62, 99], [30, 52], [27, 69], [62, 52], [42, 77], [59, 87], [21, 39], [18, 16],
              [48, 7], [71, 28], [75, 77], [65, 69], [59, 28], [85, 55], [22, 99], [68, 59], [80, 29], [55, 96],
              [45, 15], [41, 26], [66, 68], [3, 85], [57, 15], [20, 81], [66, 67], [63, 8], [78, 59], [13, 36],
              [51, 23], [0, 46], [31, 30], [59, 95], [42, 32], [51, 34], [64, 38], [11, 0], [40, 6], [58, 47], [43, 38],
              [21, 22], [4, 38], [90, 3], [42, 26], [9, 42], [46, 67], [92, 74], [54, 65], [69, 9], [10, 2], [49, 62],
              [49, 97], [84, 28], [69, 86], [40, 28], [58, 18], [67, 39], [9, 41], [58, 23], [55, 61], [17, 30],
              [63, 9], [11, 23], [66, 56], [56, 21], [59, 24], [85, 19], [28, 69], [61, 55], [14, 77], [81, 86],
              [95, 26], [7, 53], [97, 46], [11, 31], [58, 42], [74, 67], [96, 53], [53, 49]]

    # 绘图展示以上数据
    data = np.array(o_data)
    plot(data, './echart_html/k-means原始数据.html')
    # 划分类别
    km = KMeans(n_clusters=3)
    km.fit(data)
    labels = km.labels_  # 每个数据所属类别
    centers = km.cluster_centers_  # 每个分类的中心点位置
    # print(centers)

    # pre = km.predict([[20, 10], [100, 100]])
    # print(pre)
    # 性能评估
    score = silhouette_score(data, labels)  # silhouette 轮廓   以轮廓系数来判别效果如何
    print(score)

    A = list()
    B = list()
    C = list()
    for index, lable in enumerate(labels):
        if lable == 0:
            A.append(o_data[index])
        elif lable == 1:
            B.append(o_data[index])
        else:
            C.append(o_data[index])
    A = np.array(A)
    B = np.array(B)
    C = np.array(C)

    scatter = Scatter("散点图示例")
    scatter.add("A", A[:, 0], A[:, 1])
    scatter.add("B", B[:, 0], B[:, 1])
    scatter.add("C", C[:, 0], C[:, 1])
    scatter.add("CENTER", centers[:, 0], centers[:, 1])

    scatter.render(path='./echart_html/k-means分类后.html')


def k2():
    """
    手写数字聚类过程
    :return: None
    """
    # 加载数据
    ld = load_digits()
    # print(ld.data.shape)  # (1797, 64)
    # 聚类
    k_score = dict()
    for i in range(2, 10):
        km = KMeans(n_clusters=i)

        # km.fit_transform(ld.data)
        km.fit(ld.data)
        k_score[silhouette_score(ld.data, km.labels_)] = i
        print('当前的n为：%s' % i, silhouette_score(ld.data, km.labels_))
    # 从k_score中取出最大值
    print('最佳轮廓系数时的n值: %s' % k_score[max(k_score.keys())])


if __name__ == '__main__':
    # k()
    k2()
